<?php
namespace Framework\Extensions;

use Framework\Http\Seesion;
use Framework\Redis\Connection;
use Redis;

class RedisSession extends Seesion
{
	protected $customStorage = true;

    private $keyPrefix = 'app_session_';

    public $redis = 'redis';

	public function init()
	{
		//链接redis
		// $this->redis = new Redis();
		// $this->redis->connect('127.0.0.1', 6379);


		$this->redis = new Connection();
	}


    protected function calculateKey($id)
    {
        return $this->keyPrefix . md5(json_encode([__CLASS__, $id]));
    }

    public function read($id) {
    	$key = $this->calculateKey($id);
        $data = $this->redis->get($key);
        return $data === false || $data === null ? '' : $data;
    }

    public function write($id, $data) {
    	// var_dump('--'.$data.'---');
    	$key = $this->calculateKey($id);
    	$this->redis->set($key, $data);
    	$this->redis->expire($key, $this->getTimeout());
        return true;
    }

    public function destroy($sessionId) {
    	$this->redis->del( $this->calculateKey($id) );
        return true;
    }
}